package com.authine.cloudpivot.ext.kingdee.work;

import com.alibaba.cola.dto.SingleResponse;
import com.authine.cloudpivot.ext.constants.KingDeeConstant;
import com.authine.cloudpivot.ext.dto.kingDee.request.TemporaryDTO;
import com.authine.cloudpivot.ext.dto.kingDee.request.TemporaryInvoiceDTO;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: liwei
 * @DateTime: 2021/7/6
 * @Description: TODO 其他生成凭证服务
 */
@Slf4j
public class OtherWork extends BaseWork {


    public SingleResponse deleteRecordingVoucherById( String id) {
        log.info("通过凭证列表ID删除该凭证  request:{}", id);

        int delete =  recordVoucherRepository.del(id);
        log.info("通过凭证列表ID删除该凭证 结束  delete:{}", delete);

        return SingleResponse.buildSuccess();
    }

    //    通过凭证列表ID删除该凭证：/delete/recordings
    public SingleResponse deleteRecordingVoucherByIds( List<String> ids) {
        log.info("通过凭证列表ID删除该凭证  request:{}", ids);

        ids.forEach(e->{

            int delete =  recordVoucherRepository.del(e);
            log.info("通过凭证列表ID删除该凭证 结束 delete:{}", delete);

        });

        return SingleResponse.buildSuccess();
    }

    //    开平台客户蓝票、红票、供应商服务费蓝票、红票暂存：/save/temporaryding
    public SingleResponse saveTemporary( TemporaryDTO temporaryDTO) {
        log.info("开平台客户蓝票、红票、供应商服务费蓝票、红票暂存  request:{}", temporaryDTO);

        Map<String, Object> data = new HashMap<>();
        data.put(KingDeeConstant.BUSINESS_ID_FIELD,temporaryDTO.getId());
        data.put(KingDeeConstant.BUSINESS_TYPE_FIELD, temporaryDTO.getType().name());
        data.put(KingDeeConstant.INVOICE_NO_FIELD, temporaryDTO.getNumber());
        data.put(KingDeeConstant.IS_SUCCESS_FIELD, false);
        data.put(KingDeeConstant.ACCOUNT_NAME_FIELD, this.getPlatformName(temporaryDTO.getAccountName()));

        int insert = recordVoucherRepository.create(data);
        log.info("开平台客户蓝票、红票、供应商服务费蓝票、红票暂存 凭证保存结束  insert:{}", insert);

        return SingleResponse.buildSuccess();
    }

    //    开平台客户蓝票、红票、供应商服务费蓝票、红票暂存：/save/temporary/invoice
    public SingleResponse saveTemporaryInvoice( TemporaryInvoiceDTO temporaryInvoiceDTO) {
        log.info("开平台客户蓝票、红票、供应商服务费蓝票、红票暂存  request:{}", temporaryInvoiceDTO);

        Map<String, Object> data = new HashMap<>();
        data.put(KingDeeConstant.BUSINESS_ID_FIELD,temporaryInvoiceDTO.getId());
        data.put(KingDeeConstant.BUSINESS_TYPE_FIELD, temporaryInvoiceDTO.getType().name());
        data.put(KingDeeConstant.INVOICE_NO_FIELD, temporaryInvoiceDTO.getNumber());
        data.put(KingDeeConstant.IS_SUCCESS_FIELD, false);
        data.put(KingDeeConstant.ACCOUNT_NAME_FIELD, this.getPlatformName(temporaryInvoiceDTO.getAccountName()));

        //重新写入一条新的数据
        int insert = recordVoucherRepository.create(data);
        log.info("开平台客户蓝票、红票、供应商服务费蓝票、红票暂存 保存数据结束  insert:{}", insert);

        return SingleResponse.buildSuccess();
    }

    //    通过凭证记录ID删除该凭证记录：/delete/voucher/

    public SingleResponse deleteVoucherById(@Parameter List<String> ids) {
        log.info("通过凭证记录ID删除该凭证记录  request:{}", ids);

        List<Map<String, Object>> mapList = new ArrayList<>();
        ids.forEach(e->{
            //todo 使用框架删除
            int delete = voucherRepository.del(e);
            log.info("通过凭证记录ID删除该凭证记录 结束 delete:{}", delete);
        });

        return SingleResponse.buildSuccess();
    }


}
